<script setup lang="ts">
import { downloadApp } from "~/api/user";
import { useImageCache } from "~/composables/useImageCache";

useSeoMeta({
  title: "下载APP - 技能猴子",
  ogTitle: "下载APP - 技能猴子",
  description: "技能猴子，找技能服务，我喜欢和技能人才直接聊",
  ogDescription: "技能猴子，找技能服务，我喜欢和技能人才直接聊",
});

const whiteHeaderMenu = ref(false);
const pageScroll = () => {
  let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  if (scrollTop >= 200) {
    whiteHeaderMenu.value = true;
  } else {
    whiteHeaderMenu.value = false;
  }
};

onMounted(() => {
  document.addEventListener("scroll", pageScroll);
  getApp();
});
onUnmounted(() => {
  document.removeEventListener("scroll", pageScroll);
});

const app = ref([]);
const loading = ref(true);
const { getCoverImage } = useImageCache();

const getApp = async () => {
  try {
    loading.value = true;
    const res = await downloadApp();
    app.value = res.data;
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
};

// 导入默认图片
import defaultGoodsImg from '~/assets/goods-default.png';

// 图片加载错误处理
const handleImageError = (event: Event) => {
  const target = event.target as HTMLImageElement;
  // 防止无限循环：只有当前src不是默认图片时才设置默认图片
  if (target.src !== defaultGoodsImg) {
    target.src = defaultGoodsImg;
  }
};

// 图片加载完成处理
const handleImageLoad = (event: Event) => {
  const target = event.target as HTMLImageElement;
  target.style.opacity = '1';
};
</script>

<template>
  <BaseHeaderMenuWhite bg-color="transparent" :borderBottom="false" />
  <div class="top">
    <div class="top-box">
      <h1 class="h1">专业技能，<span>触手可及</span></h1>
      <h4>快速、稳定、高效的随时随地购买你想要的技能服务</h4>
      <!-- <div class="top-btn">
				<img src="~/assets/app-qrcode.png" alt="">
				<span>扫码二维码下载APP</span>
			</div> -->
    </div>
  </div>
  <div class="apps">
    <p class="app-title">立即下载</p>
    <p class="title-tips">在所有设备上给你出色的设计体验</p>
    
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>正在加载应用列表...</p>
    </div>
    
    <!-- 空状态 -->
    <div v-else-if="!loading && app.length === 0" class="empty-state">
      <p>暂无应用可下载</p>
    </div>
    
    <!-- 应用列表 -->
    <div v-else class="list">
      <div class="item" v-for="item in app" :key="item.id">
        <img 
          class="app-icon" 
          :src="getCoverImage(item.icon)" 
          :alt="item.label"
          @error="handleImageError"
          @load="handleImageLoad"
          loading="lazy"
        />
        <p class="name">{{ item.label }}</p>
        <span class="type">{{ item.subLabel }}</span>
        <p class="line"></p>
        <p class="tips" v-html="item.introduce"></p>
        <div class="qr">
          <img 
            :src="getCoverImage(item.qrCode)" 
            :alt="`${item.label}二维码`"
            @error="handleImageError"
            @load="handleImageLoad"
            loading="lazy"
          />
        </div>
        <span class="pla">{{ item.platform }}</span>
      </div>
    </div>
  </div>
  <BaseFooter />
</template>

<style scoped lang="scss">
.top {
  min-height: 522px;
  background: url("~/assets/header-bg-img.png") no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  padding: var(--spacing-xl) 0;
  
  @media (max-width: 767px) {
    min-height: 400px;
    padding: var(--spacing-lg) 0;
  }
  
  @media (max-width: 479px) {
    min-height: 350px;
    padding: var(--spacing-md) 0;
  }
}

.top-box {
  width: 100%;
  max-width: var(--container-xl);
  margin: 0 auto;
  padding: 0 var(--spacing-md);
  
  @media (max-width: 767px) {
    padding: 0 var(--spacing-sm);
    text-align: center;
  }
  
  @media (max-width: 479px) {
    padding: 0 var(--spacing-xs);
  }
  
  .h1 {
    font-weight: 600;
    font-size: clamp(28px, 5vw, 44px);
    color: #ffffff;
    margin-bottom: var(--spacing-sm);
    line-height: 1.2;
    
    @media (min-width: 1024px) {
      padding-left: 80px;
    }
    
    span {
      color: rgba(246, 190, 86, 1);
    }
  }
  
  h4 {
    font-size: clamp(14px, 2.5vw, 16px);
    color: rgba(255, 255, 255, 0.6);
    letter-spacing: 1px;
    line-height: 1.5;
    
    @media (min-width: 1024px) {
      padding-left: 80px;
    }
  }
  
  .top-btn {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: var(--spacing-xl);
    
    @media (min-width: 1024px) {
      padding-left: 80px;
    }
    
    @media (max-width: 767px) {
      align-items: center;
    }
    
    img {
      width: 128px;
      height: 128px;
      margin-bottom: var(--spacing-sm);
      border-radius: 8px;
      
      @media (max-width: 479px) {
        width: 100px;
        height: 100px;
      }
    }
    
    span {
      color: rgba(255, 255, 255, 0.6);
      font-size: var(--font-size-sm);
    }
  }
}

.apps {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(40px, 8vw, 80px) var(--spacing-md) clamp(30px, 6vw, 60px);
  background: rgba(249, 249, 249, 1);
  
  @media (max-width: 767px) {
    padding-left: var(--spacing-sm);
    padding-right: var(--spacing-sm);
  }
  
  @media (max-width: 479px) {
    padding-left: var(--spacing-xs);
    padding-right: var(--spacing-xs);
  }
}

.app-title {
  font-weight: 600;
  font-size: clamp(20px, 4vw, 28px);
  color: #191e28;
  margin-bottom: var(--spacing-xs);
  text-align: center;
}

.title-tips {
  color: rgba(25, 30, 40, 0.48);
  font-size: clamp(14px, 2.5vw, 18px);
  margin-bottom: clamp(24px, 5vw, 48px);
  text-align: center;
  line-height: 1.5;
}

/* 加载状态样式 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(40px, 8vw, 80px) 0;
  
  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #1dbf73;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: var(--spacing-md);
  }
  
  p {
    color: rgba(25, 30, 40, 0.6);
    font-size: var(--font-size-sm);
  }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 空状态样式 */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(40px, 8vw, 80px) 0;
  
  p {
    color: rgba(25, 30, 40, 0.48);
    font-size: var(--font-size-base);
  }
}

.list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: clamp(20px, 4vw, 48px);
  width: 100%;
  max-width: 1200px;
  
  @media (max-width: 767px) {
    grid-template-columns: 1fr;
    gap: var(--spacing-lg);
  }
  
  .item {
    box-shadow: 0px 0px 20px 0px rgba(25, 30, 40, 0.06);
    border-radius: 24px;
    border: 1px solid rgba(25, 30, 40, 0.08);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: clamp(20px, 4vw, 36px);
    background: #fff;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    
    &:hover {
      transform: translateY(-4px);
      box-shadow: 0px 8px 30px 0px rgba(25, 30, 40, 0.12);
    }
    
    .app-icon {
      width: clamp(56px, 8vw, 68px);
      height: clamp(56px, 8vw, 68px);
      margin-bottom: var(--spacing-lg);
      border-radius: 12px;
      opacity: 0;
      transition: opacity 0.3s ease;
      object-fit: cover;
      
      &.loaded {
        opacity: 1;
      }
    }
    
    .name {
      font-size: clamp(16px, 3vw, 20px);
      color: #191e28;
      font-weight: bold;
      margin-bottom: var(--spacing-xs);
      text-align: center;
    }
    
    .type {
      background: rgba(29, 191, 115, 0.08);
      border-radius: 4px;
      color: #1dbf73;
      font-size: var(--font-size-xs);
      padding: 2px 8px;
      margin-bottom: var(--spacing-md);
      white-space: nowrap;
    }
    
    .line {
      width: 100%;
      height: 1px;
      background: linear-gradient(
        90deg,
        rgba(25, 30, 40, 0) 0%,
        rgba(25, 30, 40, 0.24) 50%,
        rgba(25, 30, 40, 0) 100%
      );
      margin-bottom: var(--spacing-sm);
    }
    
    .tips {
      color: rgba(25, 30, 40, 0.6);
      font-size: var(--font-size-xs);
      line-height: 1.6;
      text-align: center;
      flex-grow: 1;
      margin-bottom: var(--spacing-lg);
    }
    
    .qr {
      border-radius: 8px;
      border: 1px solid rgba(25, 30, 40, 0.08);
      width: clamp(80px, 12vw, 100px);
      height: clamp(80px, 12vw, 100px);
      margin-bottom: var(--spacing-md);
      overflow: hidden;
      background: #f9f9f9;
      display: flex;
      align-items: center;
      justify-content: center;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        opacity: 0;
        transition: opacity 0.3s ease;
        
        &.loaded {
          opacity: 1;
        }
      }
    }
    
    .pla {
      color: #191e28;
      font-size: var(--font-size-xs);
      font-weight: 500;
      text-align: center;
    }
  }
}

/* 超大屏幕优化 */
@media (min-width: 1440px) {
  .top-box {
    max-width: var(--container-xxl);
  }
  
  .list {
    max-width: 1400px;
  }
}

/* 横屏手机优化 */
@media (max-height: 500px) and (orientation: landscape) {
  .top {
    min-height: 300px;
  }
  
  .top-box .h1 {
    font-size: 32px;
  }
}
</style>
